<?php

namespace app\common\logic;

class Dictionary extends LogicCore
{
	
    //重置/获取缓存
    public function _ResetCache($_Type = false){
		cache('sys.dictionary', NULL);
        $_Data = $this->_Cache($_Type);
		if($_Type){
			return $_Data;
		}
	}
	
    //设置/获取缓存
    public function _Cache($_Type = false){
        //获取缓存中的模块列表
        $_Data = cache('sys.dictionary');
        //判断缓存是否存在
        if(!$_Data){
            //从数据库中读取开启状态的模块
            $_Data = db('Dictionary')->where(['status'=>1])->order(['sort'=>'asc'])->select();
			$_Data = $this->_Tree($_Data);
            //将读取模块数据 进行缓存
            cache('sys.dictionary',$_Data);
        }
		if($_Type){
			return $_Data;
		}
    }
	
    //列表转字典
    public function _Tree($_Data,$_Pid='abc20d7bde1df257f890e152af2e3470'){
        $_Tree = [];
		foreach($_Data as $_K => $_V){
			if($_V['p_id'] == $_Pid){
				$_Val = $this->_Tree($_Data,$_V['id']);
				if($_Val == []){
					$_Tree[] = [
						'text'=>$_V['title'],
						'value'=>$_V['value']
					];
				}else{
					$_Tree[$_V['value']] = $_Val;
				}
			}
		}
		return $_Tree;
    }
}